package com.bw.guoych;

import info.monitorenter.cpdetector.io.*;
import java.io.File;
import java.nio.charset.Charset;

public class JavaFileChasetChek {

    public static void main(String[] args){
        File file = new File("F://1.csv");
        System.out.println("Hello World!");
       System.out.println(getFileFormat(file));
    }

    private static String getFileFormat(File file){
        CodepageDetectorProxy detector = CodepageDetectorProxy.getInstance();

        detector.add(new ParsingDetector(false));
        detector.add(UnicodeDetector.getInstance());

        //内部引用了 chardet.jar的类
        detector.add(JChardetFacade.getInstance());
        detector.add(ASCIIDetector.getInstance());

        Charset charset = null;
        try {
            charset = detector.detectCodepage(file.toURI().toURL());
        } catch (Exception e) {
            e.printStackTrace();
        }
        //默认为GBK
        String charsetName = "GBK";
        if (charset != null) {
            if ("US-ASCII".equals(charset.name())) {
                charsetName = "ISO_8859_1";
            } else{
                charsetName = charset.name();
            }
        }
        return charsetName;
    }
}
